<?php
/**
 * 我的礼品
 *
 *
 *
 *
 
 */

// use Shopnc\Tpl;
defined('InShopNC') or exit('Access Invalid!');

class member_giftsControl extends mobileMemberControl
{

    public function __construct()
    {
        parent::__construct();
    }
    
    // 根据活动ID获取礼品信息
    public function member_giftsListOp()
    {
        $model_activity = Model('lottery_activity');
        $model_gift = Model('lottery_gift');
        
        $activity_id = intval($_POST['activity_id']);
        if (empty($activity_id)) {
            $activity_id = intval($_GET['activity_id']);
        }
        $condition = array();
        $condition['lottery_activity_id'] = $activity_id;
        $activity = $model_activity->getActivityInfo($condition);
        if (! empty($activity)) {
            $gifts_list = $model_gift->getGiftExtendList($condition);
            
            output_data(array(
                'activity' => $activity,
                'gift_list' => $gifts_list
            ));
        } else {
            output_error('参数不正确');
        }
    }
    
    //兑换优惠券
    public function exchange_voucherOp(){
        $gift_id = explode(',', intval($_POST ['gift_id']));
        if(empty($gift_id)) {
            output_data(array('state'=>false,'msg'=>'参数有误'));die;
        }
        
        for($i=0,$j=count($gift_id); $i<$j; $i++) {
            $model_gift = Model("lottery_gift");
            $con["lottery_gift_id"] = $gift_id[$i];
            $gift = $model_gift->getGiftInfo($con);
            
            if($gift['type'] == 1) {
                $model_voucher=Model('voucher');
                $member_id=$this->member_info['member_id'];
                $voucher_id=$gift['goods_id'];
                $condition['voucher_t_id']=$voucher_id;
                $voucher_info=$model_voucher->getVoucherTemplateList($condition);
                
                
                $check=$model_voucher->getCanChangeTemplateInfo($voucher_id,$member_id,0,0);//查询是否可以兑换
                if($check['state']){
                    //可以领取
                    //查询会员信息
                    if (!$member_name){
                        $member_info = Model('member')->getMemberInfoByID($member_id);
                        $member_name = $member_info['member_name'];
                    }
                    //添加代金券信息
                    $insert_arr = array();
                    $insert_arr['voucher_code'] = $model_voucher->get_voucher_code();
                    $insert_arr['voucher_t_id'] = $voucher_info[0]['voucher_t_id'];
                    $insert_arr['voucher_title'] = $voucher_info[0]['voucher_t_title'];
                    $insert_arr['voucher_desc'] = $voucher_info[0]['voucher_t_desc'];
                    
                    //新增处理代金券有效期
                    if ($voucher_info[0]['voucher_t_datetype'] == 1) {
                        $insert_arr['voucher_start_date'] = $voucher_info[0]['voucher_t_start_date'];
                        $insert_arr['voucher_end_date'] = $voucher_info[0]['voucher_t_end_date'];
                    } else if ($voucher_info[0]['voucher_t_datetype'] == 2) {
                        $insert_arr['voucher_start_date'] = time();
                        $insert_arr['voucher_end_date'] = $insert_arr['voucher_start_date']+$voucher_info[0]['voucher_t_cycle']*3600*24;
                    }
                    $insert_arr['voucher_price'] = $voucher_info[0]['voucher_t_price'];
                    $insert_arr['voucher_limit'] = $voucher_info[0]['voucher_t_limit'];
                    $insert_arr['voucher_store_id'] = $voucher_info[0]['voucher_t_store_id'];
                    $insert_arr['voucher_state'] = 1;
                    $insert_arr['voucher_active_date'] = time();
                    $insert_arr['voucher_owner_id'] = $member_id;
                    $insert_arr['voucher_owner_name'] = $member_name;
                    
                    $result = $model_voucher->table('voucher')->insert($insert_arr);
                    if ($result){
                        //代金券模板的兑换数增加
                        $model_voucher->editVoucherTemplate(array('voucher_t_id'=>$voucher_info[0]['voucher_t_id']), array('voucher_t_giveout'=>array('exp','voucher_t_giveout+1')));
                        wcache($member_id, array('voucher_count' => array('exp','voucher_count+1')), 'm_voucher');
                        output_data(array('state'=>true,'msg'=>'兑换成功'));
                    } else {
                        output_data(array('state'=>false,'msg'=>'兑换失败'));
                    
                    }    
                     
                }
                else{
                    output_error($check['msg'].'22');
                }
            }
        }
    }
    
    /**
     * 兑奖第一步:选择收获地址和配置方式
     */
    public function exchange_step1Op() {
        $gift_id = explode(',', $_POST['gift_id']);
         
        $logic_buy = logic('exchange');
    
        //得到购买数据
        $result = $logic_buy->buyStep1($gift_id, $this->member_info['member_id']);
        if(!$result['state']) {
            output_error($result['msg']);
        } else {
            $result = $result['data'];
        }
    
        //整理数据
        $store_cart_list = array();
        foreach ($result['store_cart_list'] as $key => $value) {
            $store_cart_list[$key]['goods_list'] = $value;
            $store_cart_list[$key]['store_goods_total'] = $result['store_goods_total'][$key];
            if(!empty($result['store_premiums_list'][$key])) {
                $result['store_premiums_list'][$key][0]['premiums'] = true;
                $result['store_premiums_list'][$key][0]['goods_total'] = 0.00;
                $store_cart_list[$key]['goods_list'][] = $result['store_premiums_list'][$key][0];
            }
            $store_cart_list[$key]['store_mansong_rule_list'] = $result['store_mansong_rule_list'][$key];
            $store_cart_list[$key]['store_voucher_list'] = $result['store_voucher_list'][$key];
            if(!empty($result['cancel_calc_sid_list'][$key])) {
                $store_cart_list[$key]['freight'] = '0';
                $store_cart_list[$key]['freight_message'] = $result['cancel_calc_sid_list'][$key]['desc'];
            } else {
                $store_cart_list[$key]['freight'] = '1';
            }
            $store_cart_list[$key]['store_name'] = $value[0]['store_name'];
        }
    
        $member_id=$this->member_info['member_id'];
        $member_data = Model('member')->getMemberInfoByID($member_id);
        $point_exchange = Model('setting')->getRowSetting('point_exchange');
        $buy_list = array();
        $buy_list['store_cart_list'] = $store_cart_list;
        $buy_list['freight_hash'] = $result['freight_list'];
        $buy_list['address_info'] = $result['address_info'];
        $buy_list['freight_total'] = $result['freight_total'];
        $buy_list['ifshow_offpay'] = $result['ifshow_offpay'];
        $buy_list['deny_edit_payment'] = $result['deny_edit_payment'];
        $buy_list['allow_offpay'] = $result['allow_offpay'];
        $buy_list['allow_offpay_batch'] = $result['allow_offpay_batch'];
        $buy_list['offpay_hash'] = $result['offpay_hash'];
        $buy_list['offpay_hash_batch'] = $result['offpay_hash_batch'];
    
        $buy_list['vat_hash'] = $result['vat_hash'];
        $buy_list['inv_info'] = $result['inv_info'];
        $buy_list['available_predeposit'] = $result['available_predeposit'];
        $buy_list['available_rc_balance'] = $result['available_rc_balance'];
        $buy_list['member_point'] = $member_data['member_points'];
        $buy_list['point_exchange'] = $point_exchange['value'];
        $buy_list['gift_id'] = $_POST['gift_id'];
        
        $buy_list['is_pick'] = $result['is_pick'];
        if($result['is_pick']) {
            $setting = Model("setting")->getListSetting();
            $buy_list['pick_address'] = $setting['delivery_area_info'].$setting['delivery_address'];
        }
        output_data($buy_list);
    }
    
    /**
     * 兑奖第二步:保存订单入库，产生订单号，开始选择支付方式
     *
     */
    public function exchange_step2Op() {
        $param = array();
        $param['gift_id'] = explode(',', $_POST['gift_id']);
        $param['address_id'] = $_POST['address_id'];
        $param['vat_hash'] = $_POST['vat_hash'];
        $param['invoice_id'] = $_POST['invoice_id'];
        $param['is_pick'] = $_POST['is_pick'];
        $param['offpay_hash'] = $_POST['offpay_hash'];
        $param['offpay_hash_batch'] = $_POST['offpay_hash_batch'];
        $param['pay_name'] = $_POST['pay_name'];
    
        //手机端暂时不做支付留言，页面内容太多了
        //$param['pay_message'] = json_decode($_POST['pay_message']);
        $param['pd_pay'] = $_POST['pd_pay'];
        $param['rcb_pay'] = $_POST['rcb_pay'];
        $param['password'] = $_POST['password'];
        $param['order_from'] = 2;
        $param['pay_name'] = 'online';
        $logic_buy = logic('exchange');
    
        $result = $logic_buy->buyStep2($param, $this->member_info['member_id'], $this->member_info['member_name'], $this->member_info['member_email']);
        if(!$result['state']) {
            output_error($result['msg']);
        }
        output_data(array('pay_sn' => $result['data']['pay_sn'],'need_pay' => $result['data']['need_pay']));
    }
}
